﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->search->setMaximumWidth(200);//设置最大宽度
    ui->search->setPlaceholderText("Search...");

    ui->tableWidget->setRowCount(5);//设置5行
    ui->tableWidget->setColumnCount(15);//设置15列
    ui->tableWidget->setAlternatingRowColors(true);//隔行变色
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//选中一整行
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据内容天凭证大小根据


}

Widget::~Widget()
{
    delete ui;
}

//添加一行
void Widget::on_btnAdd_clicked()
{
    int currentRow = ui->tableWidget->currentRow();//获取当前行
    ui->tableWidget->insertRow(currentRow + 1);//当前行下面增加一行

}

//删除一行
void Widget::on_btnDel_clicked()
{
    int currentRow = ui->tableWidget->currentRow();//获取当前行
    ui->tableWidget->removeRow(currentRow);

}

//搜索功能
void Widget::on_search_textChanged(const QString &arg1)
{
    if(arg1.isEmpty())//判断当前文本框为空
    {
        int rowCount = ui->tableWidget->rowCount();
        for(int row = 0; row < rowCount; row++)
        {
            ui->tableWidget->showRow(row);//显示所有行
        }
        return;
    }

    int rowCount = ui->tableWidget->rowCount();//获取行数
    int columnCount = ui->tableWidget->columnCount();//获取列数
    for(int row = 0; row < rowCount; row++)//循环每一行
    {
        QString rowData;
        for(int column = 0; column < columnCount; column++)//循环每一列
        {
            if(ui->tableWidget->item(row,column))
            {
                rowData += ui->tableWidget->item(row,column)->text();//变量拼接存入rowData
            }
        }
        if(!rowData.isEmpty())//有数据
        {
            if(rowData.contains(arg1))
            {
                ui->tableWidget->showRow(row);//显示行
            }
            else
            {
                ui->tableWidget->hideRow(row);//隐藏行
            }
        }
        else//无数据
        {
            ui->tableWidget->hideRow(row);//隐藏行
        }
    }
}
